
package com.platform.modules.mch.controller;

import com.alibaba.fastjson.JSONObject;
import com.platform.common.utils.RestResponse;
import com.platform.common.utils.ValidUtils;
import com.platform.modules.mall.entity.MallGoodsEntity;
import com.platform.modules.mall.service.MallGoodsService;
import com.platform.modules.mch.dto.MchGoodsNumberLockDto;
import com.platform.modules.mch.service.MchGoodsService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 商品库存锁定
 */
@RestController
@RequestMapping("/mch/numlock")
public class MchGoodsNumberLockController {

    @Resource
    private MchGoodsService mchGoodsService;
    @Resource
    private MallGoodsService mallGoodsService;

    /**
     * 查询可用的库存数
     *
     * @return
     */
    @GetMapping("/getStock/{goodsId}")
    public RestResponse getStock(@PathVariable("goodsId") String goodsId) {
        ValidUtils.allNotNull(goodsId);
        return RestResponse.success().put("data", mchGoodsService.getStock(goodsId));
    }

    /**
     * 锁定库存
     *
     * @return
     */
    @PostMapping("/lockStock")
    public RestResponse lockStock(@RequestBody JSONObject pamas) {
        mchGoodsService.lockStock(pamas);
        return RestResponse.success();
    }

    /**
     * 库存同步
     *
     * @return
     */
    @GetMapping("/syncNumber/{goodsId}")
    public RestResponse syncNumber(@PathVariable("goodsId") String goodsId) {
        ValidUtils.allNotNull(goodsId);
        MchGoodsNumberLockDto stock = mchGoodsService.getStock(goodsId);
        if(stock.getCurrentLockNumber() >= 0){
            mallGoodsService.lambdaUpdate()
                    .set(MallGoodsEntity::getGoodsNumber,stock.getCurrentLockNumber())
                    .eq(MallGoodsEntity::getId,goodsId)
                    .update();
        }
        return RestResponse.success();
    }

}
